package com.guaju.yueshangLife.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.bean.AddAddressBean;
import com.guaju.yueshangLife.databinding.ActivityAddAddressBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.ToastUtil;
import com.guaju.yueshangLife.utils.Utils;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityPicker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class AddAddressActivity extends BaseActivity {
    private MaterialDialog dialog;
    public static String TYPE = "type";
    private boolean isDefault = false;
    private int int_province, int_city, int_district, is_default;
    private String address, contact_name, contact_phone;


    private ActivityAddAddressBinding binding;
    private int type;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_add_address);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void bindViews() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (null != extras) {
            type = extras.getInt(TYPE, 0);
        } else {
            type = 0;
        }

        if (0 == type) {
            binding.topbar.setBarTitle("添加新地址");
        } else {
            binding.topbar.setBarTitle("修改地址");
        }
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    protected void setListener() {
        binding.ivSetDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        binding.tvLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JDCityPicker cityPicker = new JDCityPicker();

                cityPicker.init(AddAddressActivity.this);
                cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
                    @Override
                    public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                        binding.tvLocation.setText(province.getName() + city.getName() + district.getName());
                        binding.tvLocation.setTextColor(getResources().getColor(R.color.grey_333));
                        int_province = Integer.parseInt(province.getId());
                        int_city = Integer.parseInt(province.getId());
                        int_district = Integer.parseInt(district.getId());
                    }

                    @Override
                    public void onCancel() {
                        binding.tvLocation.setTextColor(getResources().getColor(R.color.grey_999));
                        Toast.makeText(mContext, "选择省市区失败", Toast.LENGTH_SHORT).show();
                    }
                });
                cityPicker.showCityPicker();
            }
        });

        binding.topbar.getLeftBack().setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                dialog = new MaterialDialog.Builder(AddAddressActivity.this)
                        .title("确认离开")
                        .content("直接离开，您所填写的信息将不被保存")
                        .negativeText("取消")
                        .negativeColor(getResources().getColor(R.color.c_gray1))
                        .positiveText("确认")
                        .positiveColor(getResources().getColor(R.color.colorPrimary))
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                TODO
                                dialog.dismiss();
                            }
                        })
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                        })
                        .build();
                dialog.show();
            }
        });

        binding.topbar.getRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobile = binding.etMobile.getText().toString().trim();
                if (Utils.isMobileNO(mobile)) {

                    address = binding.etDetailLoc.getText().toString().trim();
                    contact_name = binding.etName.getText().toString().trim();
                    contact_phone = binding.etMobile.getText().toString();
                    if (TextUtils.isEmpty(address)) {
                        Toast.makeText(mContext, "请填写详细地址", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (TextUtils.isEmpty(contact_name)) {
                        Toast.makeText(mContext, "请填写收件人姓名", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (TextUtils.isEmpty(contact_phone)) {
                        Toast.makeText(mContext, "请填写收件人手机号", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        if (isDefault) {
                            is_default = 1;
                        } else {
                            is_default = 0;
                        }
                        if (1 == type) {
                            /**
                             * 修改地址
                             */

                        } else if (0 == type) {
                            ApiEntity.addAddress(int_province, int_city, int_district, address, contact_name, contact_phone, is_default);
                        }
                    }
                    finish();
                } else {
                    Toast.makeText(mContext, "手机格式不正确，请您重新输入~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        binding.ivSetDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDefault) {
                    isDefault = false;
                    binding.ivSetDefault.setBackgroundResource(R.drawable.toggle_off);
                } else {
                    isDefault = true;
                    binding.ivSetDefault.setBackgroundResource(R.drawable.toggle_on);
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAddressAdded(AddAddressBean bean) {
        if (bean == null) {
            ToastUtil.show("添加失败，请稍后重试");
        } else {
            String msg = bean.getMsg();
            ToastUtil.show(msg);
            AddAddressBean.DataBean data = bean.getData();

        }

    }
}
